﻿var XMLFile="51DiTu_E_China.xml";
var xmlDoc=null;
var xmldocAddr= new XmlDOM();
LoadXMLDOC(XMLFile);

function LoadXMLDOC(XMLFilePath)
{
    xmlDoc = new XmlDOM();
    
    xmlDoc.async = false;
    xmlDoc.load(XMLFilePath);
    if (xmlDoc.parseError.errorCode != 0) 
    {//如果打开文件出错
        var myErr = xmlDoc.parseError;
        document.write("51地图纠错文件访问出错：" + myErr.reason);
    }   
    else
    {
        xmlDoc.setProperty("SelectionLanguage", "XPath");
    }

}



/**/
//返回校正后的经纬度
function Get_ReviseSTR(oLon,oLat,Addr)
{
    var NewLon=Number(oLon);
    var NewLat=Number(oLat);
    
    //获取偏移量
    var aryReviseValue=Get_ReviseValue(oLon,oLat,Addr);
    
    //校正经度
    if(aryReviseValue[2] !=0)
        NewLon=Number(oLon).add(aryReviseValue[0]);
    //校正纬度    
    if(aryReviseValue[3]!=0)
        NewLat=Number(oLat).add(aryReviseValue[1]);　

    
    return new Array(NewLon,NewLat);
}



//返回经纬度偏移量
function Get_ReviseValue(oLon,oLat,Addr)
{
    var ReviseLon=0;
    var ReviseLat=0;
    
    //如果如地址参数为空串，需要再去51地图获取经纬度对应的地址 
    if(Addr.length==0)
        Addr=GetAddrFrom51Ditu(Number(oLon),Number(oLat));
        
    if(Addr.length>0)
    {
        if(xmlDoc!=null)
        {
            strRevise="";
            var Items = xmlDoc.documentElement.childNodes;//取得Item部分
            ReviseSTR(Items,Addr); //获取经纬度校正串
            
            //如果取到校正串
            if(strRevise.length>0)
            {
                var aryRevise=strRevise.split(',');
                if(aryRevise.length==4)
                {
                   ReviseLon=Number(aryRevise[2]);  //得到精度偏移量
                   ReviseLat=Number(aryRevise[3]);　//得到纬度偏移量
                }
            }
        }
    }
    
    return new Array(ReviseLon,ReviseLat);
}





var strRevise="";
function ReviseSTR(Items,Addr)
{   
    for(var i=0;i<Items.length;i++) //遍历每个Item
    {
        n=Items[i].getAttribute('n');//关键字名
         
        if(Addr.indexOf(n)> -1)
        {
            strRevise=Items[i].getAttribute('l');//经纬度信息
            
            if(Items[i].childNodes.length>0 ) //如果还有子节点，进入递归查找
            {
                ReviseSTR(Items[i].childNodes,Addr);
                return;      
            }
            else if(strRevise.length>0) //如果没有子节点并且已经找到匹配地址，退出递归
                return;
        } 
    }
}






function GetAddrFrom51Ditu(oLon,oLat)
{
    var strAddr="";
    
    var dituLon=Math.floor(Number(oLon).mul(100000));
    var dituLat=Math.floor(Number(oLat).mul(100000));
    var strURL="http://ls.vip.51ditu.com/mosp/gc?pos="+dituLon+","+dituLat;

	var str    = "<Root><DituURL>"+strURL+"</DituURL></Root>";
	xmlHTTP.open("POST","Get51DiTuAddr.aspx",false);
	xmlHTTP.send(str);
	if(xmlHTTP.statusText=="OK")
	{
	    xmldocAddr.loadXML(xmlHTTP.responseText);
		if(xmldocAddr.parseError.errorCode == 0)
		{
				var NodeCode = xmldocAddr.selectSingleNode("/R/code");
	            if(NodeCode)
	            {
	                if(NodeCode.text=="0")
	                {
	                    var Nodemsg = xmldocAddr.selectSingleNode("/R/msg");
	                    if(Nodemsg)
	                    {
	                        strAddr=Nodemsg.text;
	                    }
	                }
	            }

		}

	}

     return strAddr;

}



function GetAddrFrom51Ditu_ByClient(oLon,oLat)
{
    var strAddr="";
    
    var dituLon=Math.floor(Number(oLon).mul(100000));
    var dituLat=Math.floor(Number(oLat).mul(100000));
    var strURL="http://ls.vip.51ditu.com/mosp/gc?pos="+dituLon+","+dituLat;

	xmlHTTP.open("POST",strURL,false);
	xmlHTTP.send();
	if(xmlHTTP.statusText=="OK")
	{
	    xmldocAddr.loadXML(xmlHTTP.responseText);
		if(xmldocAddr.parseError.errorCode == 0)
		{
				var NodeCode = xmldocAddr.selectSingleNode("/R/code");
	            if(NodeCode)
	            {
	                if(NodeCode.text=="0")
	                {
	                    var Nodemsg = xmldocAddr.selectSingleNode("/R/msg");
	                    if(Nodemsg)
	                    {
	                        strAddr=Nodemsg.text;
	                    }
	                }
	            }

		}

	}

     return strAddr;

}




function XmlDOM() {
    //通过对象/属性检测法，判断是IE来是Mozilla
    if (window.ActiveXObject) 
    {
        var arrSignatures = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0",
                             "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument",
                             "Microsoft.XmlDom"];
                         
        for (var i=0; i < arrSignatures.length; i++)
        {
            try 
            {
                var oXmlDom = new ActiveXObject(arrSignatures[i]);
                return oXmlDom;
            } 
            catch (oError) 
            {
                //ignore
            }
        }          

        throw new Error("MSXML is not installed on your system."); 
    }
}